﻿using MvcLibrary.Repository;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VietPremier.Areas.AdminCP.Models;

namespace VietPremier.Areas.AdminCP.Controllers
{
    public class AdminBrouchureController : Controller
    {
        public ActionResult Index()
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                var query = from p in db.EntityBrouchures
                            select new AdminBrouchureModel
                            {
                                Id = p.Id,
                                Name = p.Name,
                                LinkDownload = p.LinkDownload
                            };
                if (query != null)
                {
                    return View(query.ToList());
                }
                else
                    return View("Error");
            }
        }


        #region "Edit"
        public ActionResult Edit(int id)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                var query = from p in db.EntityBrouchures
                            where p.Id == id
                            select new AdminBrouchureModel
                            {
                                Id = p.Id,
                                Name = p.Name,
                                LinkDownload = p.LinkDownload
                            };
                if (query != null)
                {
                    return View(query.FirstOrDefault());
                }
                else
                    return View("Error");
            }
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(AdminBrouchureModel model, HttpPostedFileBase FilePDF)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                EntityBrouchure Brouchure = db.EntityBrouchures.FirstOrDefault(p => p.Id == model.Id);
                if (Brouchure != null)
                {
                    if (FilePDF != null && FilePDF.ContentLength > 0)
                    {
                        int kb = FilePDF.ContentLength / 1024; //file size kb
                        if (kb >= 20480) // file qua lon
                        {
                            return RedirectToAction("Index", "Error", new { errorMsg = "Can not upload file pdf" });
                        }

                        FileHelper fileHelper = new FileHelper();
                        string encodestring = fileHelper.encodeFile(FilePDF);

                        if (encodestring == "!")
                            return RedirectToAction("Index", "Error", new { errorMsg = "Can not upload file pdf" });

                        var path = Path.Combine(Server.MapPath("~/Content/upload/brouchure/"), encodestring);
                        FilePDF.SaveAs(path);

                        Brouchure.LinkDownload = encodestring;
                    }
                    Brouchure.Name = model.Name;
                    db.SubmitChanges();
                    return RedirectToAction("Index");
                }
                else
                    return View("Error");
            }
        }
        #endregion

        #region "Insert"
        public ActionResult Insert()
        {
            return View();
        }
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Insert(AdminBrouchureModel model, HttpPostedFileBase FilePDF)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                EntityBrouchure Brouchure = new EntityBrouchure();

                Brouchure.Name = model.Name;
                if (FilePDF != null && FilePDF.ContentLength > 0)
                {
                    int kb = FilePDF.ContentLength / 1024; //file size kb
                    if (kb >= 20480) // file qua lon
                    {
                        return RedirectToAction("Index", "Error", new { errorMsg = "Can not upload file pdf" });
                    }

                    FileHelper fileHelper = new FileHelper();
                    string encodestring = fileHelper.encodeFile(FilePDF);

                    if (encodestring == "!")
                        return RedirectToAction("Index", "Error", new { errorMsg = "Can not upload file pdf" });

                    var path = Path.Combine(Server.MapPath("~/Content/upload/brouchure/"), encodestring);
                    FilePDF.SaveAs(path);

                    Brouchure.LinkDownload = encodestring;
                }
                db.EntityBrouchures.InsertOnSubmit(Brouchure);
                db.SubmitChanges();
                return RedirectToAction("Index");

            }
        }
        #endregion

        #region "Delete"
        public string Delete(int id)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                EntityBrouchure Brouchure = db.EntityBrouchures.FirstOrDefault(p => p.Id == id);
                if (Brouchure != null)
                {
                    db.EntityBrouchures.DeleteOnSubmit(Brouchure);
                    db.SubmitChanges();
                    return "Ok";
                }
                else
                    return "Error";
            }
        }
        #endregion

    }
}
